<template>
  <div class="project-list">
    <top-header></top-header>

    <div class="search-div">
      <div class="search-one-div">
        <search-one @search="onSearch"></search-one>
      </div>

      <span class="iconfont iconshaixuan"></span>
    </div>

    <div class="common-list-body">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <a href="javascript:;" @click="goto(item)" class="list-item" v-for="(item, key) in listSource" :key="key">
            <div class="name">{{ item.name }}</div>
            <div class="status">
              <span>项目进展：</span>
              <span class="status-span" :class="`status-span-${item.status}`">{{ projectStatus[item.status] }}</span>
            </div>
            <div class="date">开始服务日期：{{ item.startDate }}</div>
          </a>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script lang="ts">
import { Component } from "vue-property-decorator";
import TopHeader from "@/components/TopHeader.vue";
import { List, Cell, PullRefresh } from "vant";
import SearchOne from "@/components/SearchOne.vue";
import Api from "@/api";
import Base from "@/utils/base";
import Enum from "@/typings/Enum";

@Component({
  name: "ProjectList",
  components: {
    TopHeader,
    SearchOne,
    VanList: List,
    VanCell: Cell,
    VanPullRefresh: PullRefresh
  }
})
export default class ProjectList extends Base {
  private listSource: any[] = [];
  private loading = false;
  private finished = false;
  private refreshing = false;

  private pageNum = 1;
  private pageSize = 20;
  private sortBy = "enableFlag"; // 排序字段
  private order = "asc"; // 排序顺序
  private searchValue = ""; // 消息内容
  private leader = ""; // 项目负责人guid,必填

  private nickName = ""; // 姓名
  private jobNo = ""; // 员工编号
  private phonenumber = ""; // 搜索值
  private projectStatus = Enum.ProjectStatus;

  private onSearch(event: any) {
    this.searchValue = event;
    this.refreshing = true;
    this.onRefresh();
  }

  private onLoad() {
    if (this.finished) {
      return;
    }
    setTimeout(async () => {
      if (this.refreshing) {
        this.pageNum = 1;
        this.refreshing = false;
      }
      const res = await Api.GetProjectList(this.listParams);
      if (res && res.code === 200) {
        if (this.pageNum === 1) {
          this.listSource = res.rows;
        } else {
          this.listSource.push(...res.rows);
        }
        if (res.total <= this.listSource.length) {
          this.finished = true;
        }
        this.pageNum++;
      } else {
        this.finished = true;
      }
      this.loading = false;
    }, 500);
  }

  private onRefresh() {
    // 清空列表数据
    this.finished = false;

    // 重新加载数据
    // 将 loading 设置为 true，表示处于加载状态
    this.loading = true;
    this.onLoad();
  }

  /**
   * 跳转项目概况
   */
  private goto(item: any) {
    if (item.enableFlag === "0" && item.isvalid !== "0" && this.superIsProjectLeader) {
      this.$router.push({
        name: "ProjectDetail",
        query: { projectId: item.guid }
      });
    } else {
      this.$dialog({ message: "对不起，未启用或作废或不含项目项目负责人角色不能跳转项目主页" });
    }
  }

  get listParams() {
    const guid = this.$route.query.leader ? this.$route.query.leader : this.currentUser.userId;
    return {
      pageNum: this.pageNum,
      pageSize: this.pageSize,
      sortBy: this.sortBy,
      order: this.order,
      projectLeader: guid,
      phonenumber: this.phonenumber,
      nickName: this.nickName,
      jobNo: this.jobNo,
      searchValue: this.searchValue
    };
  }
}
</script>

<style lang="less" scoped>
.project-list {
  .common-list-body {
    height: calc(100vh - 44px - 54px);
  }
  .search-div {
    background-color: white;
    display: flex;
    align-items: center;
    .iconshaixuan {
      padding: 10px 10px 10px 0px;
      font-size: 22px;
      color: #bfbfbf;
    }
    .search-one-div {
      flex: 1;
    }
  }

  .list-item {
    margin: 10px 10px 0px 10px;
    background: #ffffff;
    border-radius: 8px;
    // height: 125px;
    display: block;
    padding: 15px;
    .name {
      font-size: 16px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      word-break: break-all;
    }
    .status {
      margin-top: 10px;
      display: flex;
      align-content: center;
      color: #717787;
    }
    .status-span {
      padding: 0 6px;
      height: 18px;
      line-height: 18px;
      text-align: center;
      font-size: 12px;
      color: white;
      border-radius: 18px;
      background: #36cfc9;
    }
    .status-span-0 {
      background: #36cfc9;
    }
    .status-span-3 {
      background: #9ea9bc;
    }
    .date {
      color: #a1a6b3;
      margin-top: 8px;
    }
  }
}
</style>
